package org.jbpm.pvm.internal.lob;

import java.sql.SQLException;

import org.hibernate.SessionFactory;
import org.jbpm.api.JbpmException;

import com.wolfpire.system.common.util.SpringContextUtil;

/**
 * 重写jbpm的BlobStrategyBlob
 * @author lihd
 *
 */
public class BlobStrategyBlob implements BlobStrategy {

	public void set(byte[] bytes, Lob lob) {
		if (bytes != null) {
			lob.cachedBytes = bytes;
//			 lob.blob = Hibernate.createBlob(bytes);// --源码(hinernate3)--seven
			// hibernate4中 Hibernate.createBlob()方法已经过时,不可用
			 lob.blob = ((SessionFactory)SpringContextUtil.getBean("sessionFactory")).getCurrentSession().getLobHelper().createBlob(bytes);

		}
	}

	public byte[] get(Lob lob) {
		if (lob.cachedBytes != null) {
			return lob.cachedBytes;
		}

		java.sql.Blob sqlBlob = lob.blob;
		if (sqlBlob != null) {
			try {
				return sqlBlob.getBytes(1, (int) sqlBlob.length());
			} catch (SQLException e) {
				throw new JbpmException("couldn't extract bytes out of blob", e);
			}
		}
		return null;
	}
}
